<%@page import="com.dilean.api.dao.ConversationDAO"%>
<%@page import="com.dilean.api.model.Conversation"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%-- imports --%>
<%@page import="com.dilean.api.dao.AttributeTypeDAO"%>
<%@page import="com.dilean.impl.model.AttributeTypeImpl"%>
<%@page import="com.dilean.api.model.AttributeType"%>
<%@page import="com.dilean.spring.AplicationContextHolder"%>
<%@page import="com.dilean.api.dao.PersonDAO"%>
<%@page import="com.dilean.impl.model.PersonImpl"%>
<%@page import="com.dilean.api.model.Person"%>
<%@page import="com.dilean.api.model.Person"%>
<%@page import="com.dilean.impl.model.AttributeListValueImpl"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueDate"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueList"%>
<%@page import="java.math.BigInteger"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueTextMultiple"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueText"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueNumber"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueNumberMultiple"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueText"%>
<%@page import="java.util.Collection"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="com.dilean.spring.AplicationContextHolder"%>
<%@page import="com.dilean.api.dao.PersonDAO"%>
<%@page import="com.dilean.impl.model.PersonImpl"%>
<%@page import="com.dilean.api.model.Person"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueListMultiple"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueList"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueDate"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueNumberMultiple"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueTextMultiple"%>
<%@page import="com.dilean.impl.model.attrval.AttrValueText"%>

<!DOCTYPE html>
<html>
    <head>

        <meta charset="utf-8">
        <title>Conversations &middot; Dilean</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="">
        <meta name="author" content="">


        <style>

            .user-row {
                margin-bottom: 14px;
            }

            .user-row:last-child {
                margin-bottom: 0;
            }

            .dropdown-user {
                margin: 13px 0;
                padding: 5px;
                height: 100%;
            }

            .dropdown-user:hover {
                cursor: pointer;
            }

            .table-user-information > tbody > tr {
                border-top: 1px solid rgb(221, 221, 221);
            }

            .table-user-information > tbody > tr:first-child {
                border-top: 0;
            }


            .table-user-information > tbody > tr > td {
                border-top: 0;
            }



        </style>

        <link href="resources/mytheme/css/bootstrap.min.css" rel="stylesheet">


        <link href="resources/mytheme/css/bootstrap.css" rel="stylesheet">
        <!-- Custom CSS -->
        <link href="resources/mytheme/css/simple-sidebar.css" rel="stylesheet">
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
        <link href="../resources/mytheme/css/bootstrap.min.css" rel="stylesheet">
        <script src="../resources/mytheme/js/jquery.bootstrap-duallistbox.js"></script>

        <link href="resources/mytheme/css/font-awesome.css" rel="stylesheet">

        <link href="resources/mytheme/css/font-awesome.min.css" rel="stylesheet">
    </head>
    <body>

        <%

            Person me = new PersonImpl();
            Person me1 = new PersonImpl();
            final int myId = 1;
            me = AplicationContextHolder.getInstance().getContext().getBean(PersonDAO.class).findByIdWithConversations(myId);
            me1 = AplicationContextHolder.getInstance().getContext().getBean(PersonDAO.class).findByIdWithFriends(myId);
            Collection<Conversation> conversations = me.getConversations();
            AttrValueText firstName;
            AttrValueText lastName;

            Collection<Person> friendRequests = me1.getFriendRequests();
            Collection<Person> friends = me1.getFriends();
            int numberRequest = 0;
            for (Person fr : friendRequests) {
                if (friends.contains(fr) == false) {
                    numberRequest++;
                }
            }

            Collection<Person> allUsers = AplicationContextHolder.getInstance().getContext().getBean(PersonDAO.class).findAll();

        %>



        <div id="wrapper">

            <!-- Sidebar -->
            <div id="sidebar-wrapper">
                <ul class="sidebar-nav">
                    <li class="sidebar-brand">
                        <a href="http://localhost:8080/Dilean/myPage.jsp">
                            <h2 align="centre" >Dilean</h2>
                        </a>
                    </li>
                    <li>
                        <a href="http://localhost:8080/Dilean/myPage.jsp"><i class="fa fa-home fa-fw-5x"></i>&nbsp; My Page</a>
                    </li>
                    <li>
                        <a href="http://localhost:8080/Dilean/friends.jsp?id=1"><i class="fa fa-users"></i>&nbsp; Friends </a>
                    </li>

                    <li>
                        <a href="http://localhost:8080/Dilean/allUsers.jsp?id=1"><i class="fa fa-database"></i>&nbsp; Users 
                            <%                                if (numberRequest != 0) {
                            %>
                            <span class="badge badge-info"><%=numberRequest%> </span>
                            <%
                                }
                            %>
                        </a>
                    </li>



                    <li>
                        <a href="http://localhost:8080/Dilean/conversation.jsp"><i class="fa fa-envelope"></i>&nbsp; Messages </a>
                    </li>
                    <li class="divider"></li>
                    <li>
                        <a href="http://localhost:8080/Dilean/editUser.jsp?id=1"><i class="fa fa-cog"></i>&nbsp; Settings</a>
                    </li>
                    <li>
                        <a href="#"><i class="fa fa-book"></i>&nbsp; About</a>
                    </li>
                    <li>
                        <a href="#"><i class="fa fa-sign-out"></i>&nbsp; LogOut</a>
                    </li>

                </ul>
            </div>
            <div id="page-content-wrapper">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-lg-12">
                            <a class="btn btn-large btn-primary" href="#menu-toggle" id="menu-toggle" >
                                <i class="fa fa-user fa-3x"></i> <h4>User Menu</h4></a>
                            <br>
                            <br>
                            <br>





                            <br><br>
                            <div class="container">
                                <div class="well col-xs-8 col-sm-8 col-md-8 col-lg-8 col-xs-offset-2 col-sm-offset-2 col-md-offset-2 col-lg-offset-2">




                                    <%      for (Conversation conv : conversations) {
                                            Collection<Person> convPersons = AplicationContextHolder.getInstance().getContext().getBean(ConversationDAO.class).findByIdWithPersons(conv.getId()).getPersons();

                                    %>

                                    <div class="row user-row">
                                        <div class="col-xs-8 col-sm-9 col-md-10 col-lg-10">
                                            <h4 ><%=conv.getName()%></h4>

                                        </div>
                                        <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1 dropdown-user" data-for=".cyruxx">
                                            <i class="glyphicon glyphicon-chevron-down text-muted"></i>
                                        </div>
                                    </div>


                                    <div class="row user-infos cyruxx">
                                        <div class="col-xs-12 col-sm-12 col-md-10 col-lg-10 col-xs-offset-0 col-sm-offset-0 col-md-offset-1 col-lg-offset-1">
                                            <div class="panel panel-primary">
                                                <div class="panel-heading">
                                                    <h3 class="panel-title">Conversation information</h3>
                                                </div>
                                                <div class="panel-body">
                                                    <div class="row">


                                                        <div class=" col-md-9 col-lg-9 hidden-xs hidden-sm">
                                                            <strong><%=conv.getName()%></strong><br>
                                                            <table class="table table-user-information">
                                                                <tbody>
                                                                    <tr>
                                                                        <td>Users:</td>
                                                                        <td>
                                                                            <%
                                                                                for (Person convPers : convPersons) {

                                                                                    firstName = convPers.getAttributeValue(1, AttrValueText.class);
                                                                                    lastName = convPers.getAttributeValue(2, AttrValueText.class);

                                                                                    String fullname = "User #" + convPers.getId();

                                                                                    if ((firstName != null) && (lastName != null)) {
                                                                                        fullname = firstName.getValue() + " " + lastName.getValue();
                                                                                    } else if ((firstName == null) && (lastName != null)) {
                                                                                        fullname = lastName.getValue();
                                                                                    } else if ((firstName != null) && (lastName == null)) {
                                                                                        fullname = firstName.getValue();
                                                                                    }
                                                                            %>  
                                                                            <a href="http://localhost:8080/Dilean/friend.jsp?id=<%=convPers.getId()%>"><%=fullname%>
                                                                                <br>

                                                                                <%
                                                                                    }
                                                                                %>




                                                                                </td>
                                                                                </tr>

                                                                                </tbody>
                                                                                </table>
                                                                                </div>
                                                                                </div>
                                                                                </div>
                                                                                <div class="panel-footer">




                                                                                    <a href="http://localhost:8080/Dilean/chat.jsp?con=<%=conv.getId()%>" class="btn btn-lg btn btn-primary btn-block">Enter</a>
                                                                                </div>
                                                                                </div>
                                                                                </div>
                                                                                </div>


                                                                                <%    }
                                                                                %>











                                                                                <div class="row user-row">

                                                                                    <div class="col-xs-8 col-sm-9 col-md-10 col-lg-10">
                                                                                        <strong>Create new Conversation</strong><br>

                                                                                    </div>
                                                                                    <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1 dropdown-user" data-for=".user3">
                                                                                        <i class="glyphicon glyphicon-chevron-down text-muted"></i>
                                                                                    </div>
                                                                                </div>





                                                                                <div class="row user-infos user3">
                                                                                    <div class="col-xs-12 col-sm-12 col-md-10 col-lg-10 col-xs-offset-0 col-sm-offset-0 col-md-offset-1 col-lg-offset-1">
                                                                                        <form  class="form-horizontal" role="form" name="myForm" action="http://localhost:8080/Dilean/newConversationAction.jsp" onsubmit="return(validate());" >
                                                                                            <div class="panel panel-primary">
                                                                                                <div class="panel-heading">
                                                                                                    <h3 class="panel-title">User information</h3>
                                                                                                </div>
                                                                                                <div class="panel-body">
                                                                                                    <div class="row">



                                                                                                        <div class=" col-md-9 col-lg-9 hidden-xs hidden-sm">
                                                                                                            <strong>New Conversation</strong><br>
                                                                                                            <table class="table table-user-information">
                                                                                                                <tbody>
                                                                                                                    <tr>
                                                                                                                        <td>Conversation name:</td>
                                                                                                                        <td> <input class="form-control" placeholder="Conversation Name" name="convName" type="text" value=""></td>
                                                                                                                    </tr>

                                                                                                                    <tr>
                                                                                                                        <td>Users:</td>
                                                                                                                    </tr>
                                                                                                                    <%
                                                                                                                        for (Person friend : allUsers) {
                                                                                                                            int id = friend.getId();
                                                                                                                            firstName = friend.getAttributeValue(1, AttrValueText.class);
                                                                                                                            lastName = friend.getAttributeValue(2, AttrValueText.class);
                                                                                                                            String fullname = "User #" + id;

                                                                                                                            if ((firstName != null) && (lastName != null)) {
                                                                                                                                fullname = firstName.getValue() + " " + lastName.getValue();
                                                                                                                            } else if ((firstName == null) && (lastName != null)) {
                                                                                                                                fullname = lastName.getValue();
                                                                                                                            } else if ((firstName != null) && (lastName == null)) {
                                                                                                                                fullname = firstName.getValue();
                                                                                                                            }
                                                                                                                    %>
                                                                                                                    <tr>
                                                                                                                        <td></td>
                                                                                                                        <td>
                                                                                                                            <label><input type="checkbox" name="us<%=id%>"><%=fullname%> </label>
                                                                                                                        </td>
                                                                                                                    </tr>
                                                                                                                    <%
                                                                                                                        }
                                                                                                                    %>



                                                                                                                </tbody>

                                                                                                            </table>
                                                                                                        </div>
                                                                                                    </div>
                                                                                                </div>

                                                                                                <div class="panel-footer">
                                                                                                    <input type="submit" class="btn btn-lg btn btn-primary btn-block" value="New Conversation">

                                                                                                </div>
                                                                                            </div>

                                                                                        </form>
                                                                                    </div>
                                                                                </div>
                                                                                </div>
                                                                                </div>






                                                                                </div>
                                                                                <!-- /#wrapper -->

                                                                                <!-- jQuery Version 1.11.0 -->
                                                                                <script src="resources/mytheme/js/jquery-1.11.0.js"></script>

                                                                                <!-- Bootstrap Core JavaScript -->
                                                                                <script src="resources/mytheme/js/bootstrap.min.js"></script>

                                                                                <!-- Menu Toggle Script -->
                                                                                <script>
                                                                                    $("#menu-toggle").click(function(e) {
                                                                                        e.preventDefault();
                                                                                        $("#wrapper").toggleClass("toggled");
                                                                                    });

                                                                                    $(document).ready(function() {
                                                                                        var panels = $('.user-infos');
                                                                                        var panelsButton = $('.dropdown-user');
                                                                                        panels.hide();

                                                                                        //Click dropdown
                                                                                        panelsButton.click(function() {
                                                                                            //get data-for attribute
                                                                                            var dataFor = $(this).attr('data-for');
                                                                                            var idFor = $(dataFor);

                                                                                            //current button
                                                                                            var currentButton = $(this);
                                                                                            idFor.slideToggle(400, function() {
                                                                                                //Completed slidetoggle
                                                                                                if (idFor.is(':visible'))
                                                                                                {
                                                                                                    currentButton.html('<i class="glyphicon glyphicon-chevron-up text-muted"></i>');
                                                                                                }
                                                                                                else
                                                                                                {
                                                                                                    currentButton.html('<i class="glyphicon glyphicon-chevron-down text-muted"></i>');
                                                                                                }
                                                                                            })
                                                                                        });


                                                                                        $('[data-toggle="tooltip"]').tooltip();

                                                                                        $('button').click(function(e) {
                                                                                            e.preventDefault();
                                                                                            alert("This is a demo.\n :-)");
                                                                                        });
                                                                                    });

                                                                                    function validate()
                                                                                    {

                                                                                        if (document.myForm.convName.value == "")
                                                                                        {
                                                                                            myForm.convName.value = "";
                                                                                           
                                                                                            alert("Conversation Name must be not empty!");
                                                                                            return false;
                                                                                            
                                                                                        }
                                                                                        else {
                                                                                            return true;
                                                                                        }
                                                                                        return true;
                                                                                    }
                                                                                    ;
                                                                                </script>
                                                                                </div>
                                                                                </div>
                                                                                </div>
                                                                                </div>
                                                                                </body>
                                                                                </html>

